package com.wcg.mapper;

import com.wcg.javabean.user;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {
    List<user> selectAll();
    List<user> selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    @Insert("insert into user(username,password,phone) values(#{username},#{password},#{phone})")
    int insertUser(user user);

    @Delete("delete from user where uid=#{uid}")
    int deleteByUid(int uid);

    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePasswordByUid(@Param("uid") int uid, @Param("password") String password);

    @Select("select * from user where uid=#{uid}")
    user selectByUid(int uid);

    @Select("select uid,username uname,password pwd,phone tel from user")
    @Results(id = "u", value = {
            @Result(id = true, column = "uid", property = "uid"),
            @Result(column = "uname", property = "username"),
            @Result(column = "pwd", property = "password"),
            @Result(column = "tel", property = "phone")
    })
    List<user> selectUser();

    @Select("select uid,username uname,password pwd,phone tel from user")
    @ResultMap("u")
    List<user> selectUser1();

    @Select("select * from user")
    @Results(id = "u_r", value = {
            @Result(id = true, column = "uid", property = "uid"),
            @Result(column = "uid", property = "r", one = @One(
                    select = "mapper.RoleMapper.selectByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    List<user> selectUser_Role();

    @Select({"        select * from user\n" +
            " <where> " +
            "            <if>and uid=#{uid}</if>\n" +
            "            <if>and username=#{username}</if>\n" +
            "            <if>and password=#{password}</if>\n" +
            "            <if>and phone=#{phone}</if>\n" +
            "        </where>"})
    List<user> selectByUser(user user);
}